package com.admin;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.BaseAction;




public class MenuService extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8528769499842428553L;
	private  String table="\"Menu\"";
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	protected  List<Menu> selectMenus() {
		
		String where="1=1";
		
		Map map=new HashMap();
		map.put("table", table);
		map.put("where", where);
		List<Menu> menus =selectmenusbysql(map);
		return menus;
	}
	
	
	
	//条件拼接
	@SuppressWarnings({ "unchecked", "rawtypes" })
	protected  List<Menu> selectMenus(Map pharm) {
		
		String where="1=1";
		Iterator<String> iter = pharm.keySet().iterator();
		while(iter.hasNext()){
			   String key=iter.next();
			   String value = (String) pharm.get(key);
			   where+=" and \""+key+"\"='"+value+"'";
		}
		Map map=new HashMap();
		map.put("table", table);
		map.put("where", where);
		List<Menu> menus =selectmenusbysql(map);
		return menus;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	protected  List<Menu> selectMenuss() {
		
		String where="1=1";
		
		Map map=new HashMap();
		map.put("table", table);
		map.put("where", where);
		List<Menu> menus =selectmenusbysql(map);
		return menus;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected   List<Menu> selectmenusbysql(Map map) {
		SqlSession sqlSession = getSqlSession();
		List<Menu> menus=new ArrayList(); 
    	try {
			MenuMapper menump=sqlSession.getMapper(MenuMapper.class);
			List<Menu>	menulist=menump.selectmenusbysql(map);
			String sjname="";
			
			List<Menu> menuxx=new ArrayList(); 
			int l=0;
			for(int n=0;n<menulist.size();n++) {
	    		
	    		
		    	Menu sjmenu=new Menu();
		    	if(sjname.equals(menulist.get(n).getSjmenuname())) {
		    		menuxx.add(menulist.get(n));
		    		
		    	}else {
		    		
		    		if(l>0) {
		    			
		    			menus.get(l-1).setSubMenus(menuxx);
	                		
		    		}
		    		
    	    			menuxx=new ArrayList(); 
    	    		
		    		
		    		sjname=menulist.get(n).getSjmenuname();
			    	sjmenu.setSjmenuname(sjname);
			    	sjmenu.setSjIcons(menulist.get(n).getSjIcons());
			    	menus.add(sjmenu);
		    		menuxx.add(menulist.get(n));
		    		l+=1;
                	
		    	}
		    	
		    }
			
				menus.get(l-1).setSubMenus(menuxx);
   
            
		 } finally {
			sqlSession.close();
		}
		return menus;
	}
}
